// pages/cart/cart.js
const api = require('../../../utils/api') // 引入API
const {
  myRequest
} = require('../../../utils/myRequest') // 请求函数
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [],
    cartList: [], // 购物车列表数据
    cartTotal: {}, // 价格，数量等
    isEditor: true // 编辑
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 购物车列表数据
    myRequest({
      url: api.cartIndex,
      success: res => {
        console.log(res);

        let cartList = res.data.data.cartList
        let cartTotal = res.data.data.cartTotal
        this.setData({
          cartList: cartList,
          cartTotal: cartTotal
        })
      }
    })
    // 关联商品详情
    myRequest({
      url: api.goodsRelated,
      data: {
        id: 1181000
      },
      success: res => {
        // console.log('商品详情页的关联商品（大家都在看）', res);
        let goodsList = res.data.data.goodsList

        this.setData({
          goodsList: goodsList,
        })
      }
    })

    // 选择或取消选择商品
    myRequest({
      url: api.cartChecked,
      data: {
        productIds: 199,
        isChecked: 0
      },
      method: 'post',
      success: res => {
        console.log(res);

      }
    })
  },
  // 页面打开显示购物车shuju
  onShow() {
    myRequest({
      url: api.cartIndex,
      success: res => {
        let cartList = res.data.data.cartList
        let cartTotal = res.data.data.cartTotal
        this.setData({
          cartList: cartList,
          cartTotal: cartTotal
        })
      }
    })
  },
  // 删除购物车的商品
  deleteGoods(e) {
    const {
      cartList
    } = this.data;
    let index,
        productIds = ""
    for (var k in cartList) {
      let item = cartList[k]
      if (item.checked == 1) {
        index = k;
        productIds = cartList[index].product_id
      }
    }
    console.log(index,productIds);
    myRequest({
      url: api.cartDelete,
      data: {
        // 字符串形式的id
        productIds: JSON.stringify(productIds)
      },
      method: 'post',
      success: res => {
        if (index == undefined) {
          wx.showToast({
            title: '请选择删除的商品',
            icon: 'none',
          });
        } else {
          cartList.splice(index, 1)
        }
        this.setData({
          cartList: cartList
        });
      }
    })
  },
  // 编辑
  editor() {
    const {
      isEditor
    } = this.data
    this.setData({
      isEditor: !isEditor
    })
  },
  //数量减少函数
  deleteCount(e) {
    const {
      cartList
    } = this.data;
    let index = e.currentTarget.dataset.index;
    var num = cartList[index].number; // 每次减一
    num = num - 1;
    cartList[index].number = num; 
    // 更新购物车的商品
    if (cartList[index].number >= 1) {
      myRequest({
        url: api.cartUpdate,
        data: {
          id: cartList[index].id,
          goodsId: cartList[index].goods_id,
          number: cartList[index].number,
          productId: cartList[index].product_id
        },
        method: 'post',
        success: res => {
          let cartList = res.data.data.cartList;
          let cartTotal = res.data.data.cartTotal;
          this.setData({
            cartList: cartList,
            cartTotal: cartTotal
          })
        }
      })
    }

  },
  // 商品数量增加
  addCount(e) {
    const {
      cartList
    } = this.data;
    let index = e.currentTarget.dataset.index;
    var num = cartList[index].number; // 每次加一
    num = num + 1;
    cartList[index].number = num;
    // 更新购物车的商品
    if (cartList[index].number >= 1) {
      myRequest({
        url: api.cartUpdate,
        data: {
          id: cartList[index].id,
          goodsId: cartList[index].goods_id,
          number: cartList[index].number,
          productId: cartList[index].product_id
        },
        method: 'post',
        success: res => {
          let cartList = res.data.data.cartList
          let cartTotal = res.data.data.cartTotal
          this.setData({
            cartList: cartList,
            cartTotal: cartTotal
          })
        }
      })
    }
  },
  // 单选
  selectList(e) {
    const {
      cartList
    } = this.data;
    let index = e.currentTarget.dataset.index;
    if (cartList[index].checked == 0) {
      cartList[index].checked = 1
    } else {
      cartList[index].checked = 0
    }
    // 选择或取消商品
    myRequest({
      url: api.cartChecked,
      data: {
        productIds: cartList[index].product_id,
        isChecked: cartList[index].checked
      },
      method: 'post',
      success: res => {
        let cartList = res.data.data.cartList
        let cartTotal = res.data.data.cartTotal 
        this.setData({
          cartList: cartList,
          cartTotal: cartTotal
        })
      }
    })
  },
  // 全选
  AllSelectList(e) {
    const {
      cartList
    } = this.data;
    let productIds = '';
    for( var index in cartList) {
      productIds += cartList[index].product_id + ','
    }
    if (this.data.cartTotal.checkedGoodsCount == 0) {
      // 选择或取消商品
      myRequest({
        url: api.cartChecked,
        data: {
          productIds: productIds,
          isChecked: 1
        },
        method: 'post',
        success: res => {
          let cartList = res.data.data.cartList
          let cartTotal = res.data.data.cartTotal 
          this.setData({
            cartList: cartList,
            cartTotal: cartTotal
          })
        }
      })
    } else {
      myRequest({
        url: api.cartChecked,
        data: {
          productIds: productIds,
          isChecked: 0
        },
        method: 'post',
        success: res => {
          let cartList = res.data.data.cartList
          let cartTotal = res.data.data.cartTotal 
          this.setData({
            cartList: cartList,
            cartTotal: cartTotal
          })
        }
      })
    }
    
  },
  onReachBottom: function () {
    wx.showLoading({
      title: '加载中...',
      duration: 1000,
    });
    const {
      goodsList
    } = this.data;
    this.data.page += 1; // 页数加1
    myRequest({
      url: api.goodsList, //品牌列表接口
      data: {
        page: this.data.page,
        size: 9
      },
      success: (res) => {
        // 数据为0是，不再请求
        if (res.data.data.data.length !== 0) {
          setTimeout(() => {
            this.setData({
              // 数据拼接
              goodsList: goodsList.concat(res.data.data.data)
            })
          }, 1200)
        } else {
          wx.hideLoading();
          wx.showToast({
            title: '已无更多内容啦~',
            icon: 'none',
            duration: 3000,
          });
          return;
        }
      },
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})